import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import vuexPersist from "vuex-persist";

export default new Vuex.Store({
  state: {
    shoplist : []
  },
  mutations: {
    add(state,item){
      let flag = false
      this.state.shoplist.forEach(list=>{
        if(list == item){
          flag = true
          list.num ++
        }
      })
      if(flag == false){
        Vue.set(item,"num",1)
        this.state.shoplist.push(item)
      }
    },
    del(state,item){

      // console.log(item);

      this.state.shoplist.forEach((list,index)=>{
        if(list._id == item._id){
          this.state.shoplist.splice(index,1)
        }
      })

    },
    count(state,sum){
      
      // console.log(sum);
      this.state.shoplist.forEach(list=>{
        if(list._id == sum.id){
          list.num = sum.val
        }
      })
    }


  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new vuexPersist({
        storage: window.localStorage,
    }).plugin,
],
})
